package com.lizheng.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lizheng.domain.ResponseResult;
import com.lizheng.domain.entity.User;
import com.lizheng.enums.AppHttpCodeEnum;
import com.lizheng.exception.SystemException;
import com.lizheng.service.BlogLoginService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Api(tags = "登录控制层")
public class BlogLoginController {

    @Resource
    private BlogLoginService blogLoginService;

    @PostMapping("/login")
    public ResponseResult login(@RequestBody User user){
        //判断参数 必须要传用户名和密码
        if(!StringUtils.isNotBlank(user.getUserName())){ //如果它为空的话
            //提示 必须要传用户名
//            throw new RuntimeException();
            throw new SystemException(AppHttpCodeEnum.REQUIRE_USERNAME);
        }

        return blogLoginService.login(user);
    }

    @PostMapping("/logout")
    public ResponseResult logout(){
        return blogLoginService.logout();
    }
}
